home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-11-25 | 9.0 KB | 280 lines | [TEXT/EDIT] |
- ; This code was written by Semantic Microsystems, Inc.
- ; which has placed it in the public domain.
-
- ; Pre-release version of the file ":Examples:texteditor.sch"
- ; from MacScheme+Toolsmith™
-
- ; A simple editing application.
-
- (begin (set! include-source-code? #f)
- (set! include-lambda-list? #f))
-
- (begin (load ":Chapters:chap20.data")
- (load ":Chapters:chap20.traps")
- (load ":Chapters:v2.chap4.data")
- (load ":Chapters:v2.chap4.traps")
- (load ":Chapters:chap7.traps")
- (load ":Examples:fs.sch")
- (load ":Examples:files.sch")
- (load ":Examples:fonts.sch")
- (load ":Examples:search.sch")
- (load ":Examples:linker.sch"))
-
- (define (main)
- (begin-application)
- (hidewindow ((lookup-window-object (frontwindow)) 'windowptr))
- (pushmenubar)
- (init-search)
- (setup-menus)
- (begin-tasking)
- (start-task idle-loop)
- (start-task relaxation-loop)
- (kill-current-task))
-
- (define (setup-menus)
- (setup-apple-menu)
- (setup-file-menu)
- (setup-edit-menu)
- (setup-font-menu)
- (setup-search-menu))
-
- (define (setup-apple-menu)
- (let ((applemenu (make-menu (list->string (list applmark)))))
- (applemenu 'addresources
- (make%restype "DRVR")
- (lambda (n)
- (lambda ()
- (let ((temp (newptr 256)))
- (getitem (applemenu 'menuhandle) n temp)
- (opendeskacc temp)
- (disposptr temp)))))))
-
- (define (setup-file-menu)
- (let ((filemenu (make-menu "File")))
- (filemenu 'append
- "New"
- (lambda () (make-document "Untitled" #f)))
- (filemenu 'append
- "Open..."
- (lambda ()
- (let ((info (stdgetfile 60 60 "TEXT")))
- (let ((flag (car info))
- (name (cadr info))
- (vrefnum (caddr info)))
- (if flag
- (let ((d (make-document name vrefnum))
- (contents (read-file name vrefnum)))
- (if contents
- ((d 'editor 'set-textstring)
- (->string contents)))))))))
- (filemenu 'append
- "Close"
- (lambda ()
- ((lookup-document-object (FrontWindow))
- 'close))
- front-window-is-ours?)
- (filemenu 'append
- "Save"
- (lambda ()
- ((lookup-document-object (FrontWindow))
- 'save))
- front-window-is-ours?)
- (filemenu 'append
- "Save as..."
- (lambda ()
- ((lookup-document-object (FrontWindow))
- 'save-as))
- front-window-is-ours?)
- (filemenu 'append "Quit" exit)))
-
- (define (setup-edit-menu)
- (let ((editmenu (make-menu "Edit")))
- (editmenu 'append
- "Undo/Z"
- (lambda () (systemedit 0))
- ; enable only if the front window is not ours
- (lambda ()
- (not (front-window-is-ours?))))
- (editmenu 'append
- "-"
- (lambda () #t)
- (lambda () #f)) ; always disable
- (editmenu 'append
- "Cut/X"
- (lambda ()
- (systemedit 2)
- ((lookup-window-object (frontwindow)) 'editor 'cut)))
- (editmenu 'append
- "Copy/C"
- (lambda ()
- (systemedit 3)
- ((lookup-window-object (frontwindow)) 'editor 'copy)))
- (editmenu 'append
- "Paste/V"
- (lambda ()
- (systemedit 4)
- ((lookup-window-object (frontwindow)) 'editor 'paste)))
- (editmenu 'append
- "Clear"
- (lambda ()
- (systemedit 5)
- ((lookup-window-object (frontwindow)) 'editor 'clear)))))
-
- (define (setup-font-menu)
- (let ((fontmenu (make-menu "Font")))
- (fontmenu 'addresources
- (make%restype "FONT")
- (lambda (n)
- (lambda ()
- (let ((temp1 (newptr 256))
- (temp2 (newptr 2)))
- (getitem (fontmenu 'menuhandle) n temp1)
- (getfnum temp1 temp2)
- (set-font (lookup-window-object (frontwindow))
- (peek.word temp2))
- (disposptr temp1)
- (disposptr temp2)))))
- (fontmenu 'append
- "-"
- (lambda () #t)
- (lambda () #f))
- (for-each (lambda (size)
- (fontmenu 'append
- (number->string size)
- (lambda ()
- (set-fontsize
- (lookup-window-object (frontwindow))
- size))
- front-window-is-ours?))
- '(9 10 12 14 18 24))))
-
- (define (setup-search-menu) (make-search-menu))
-
- ; Document objects.
- ;
- ; A document object inherits all the behavior of a window object
- ; but it has additional behavior when sent one of the following messages:
- ;
- ; window
- ; name
- ; set-name
- ; vrefnum
- ; set-vrefnum
- ; save
- ; save-as
- ; close
-
- (define (make-document name vrefnum)
- (letrec ((window (make-window 'text
- 'title name
- 'bounds 10 40 500 330))
- (self
- (lambda (op . args)
- (if args
- (apply (self op) args)
- (case op
- ((window) window)
- ((name) name)
- ((set-name)
- (lambda (newname)
- (set! name newname)
- (let ((temp (make%string name)))
- (SetWTitle (window 'windowptr) temp)
- (disposptr temp))
- name))
- ((vrefnum) vrefnum)
- ((set-vrefnum)
- (lambda (n) (set! vrefnum n) vrefnum))
- ((save)
- (if vrefnum
- (write-file name
- vrefnum
- (window 'editor 'textstring)
- "EDIT"
- "TEXT")
- (self 'save-as)))
- ((save-as)
- (let ((info (stdputfile 60 60 name)))
- (if (car info)
- (begin
- (self 'set-name (cadr info))
- (self 'set-vrefnum (caddr info))
- (self 'save)))))
- ((close)
- (set! documents
- (remove (assq window documents) documents))
- (if (not (window 'closed?))
- (window 'close)))
- (else (window op)))))))
- (set! documents (cons (list window self) documents))
- self))
-
- ; The global variable documents is an association list with elements
- ; of the form (<window-object> <document-object>).
- ; There is an entry for each window created by this application.
-
- (define documents '())
-
- ; Given a Toolbox windowptr such as is returned by FrontWindow,
- ; lookup-document-object returns the document object associated with
- ; it or #f if it's not ours.
- ; This code also redefines lookup-window-object so that it will return
- ; a document for those windows that have been created by this application.
- ; That allows documents to intercept a close message sent to a window.
-
- (define lookup-document-object)
-
- (let ((old-lookup-window-object lookup-window-object))
- (set! lookup-document-object
- (lambda (windowptr)
- (let ((entry (assq (old-lookup-window-object windowptr)
- documents)))
- (if entry
- (cadr entry)
- #f))))
- (set! lookup-window-object
- (lambda (windowptr)
- (or (lookup-document-object windowptr)
- (old-lookup-window-object windowptr))))
- #t)
-
- (define (front-window-is-ours?)
- (lookup-document-object (frontwindow)))
-
- ; Concurrent tasks.
-
- (define **task-timeslice** 500)
-
- ; This procedure soaks up idle time with occasional calls to TEIdle.
-
- (define (idle-loop)
- (call-without-interrupts
- (lambda ()
- (let ((texth ((lookup-window-object (FrontWindow))
- 'editor
- 'texthandle)))
- (if texth (teidle texth)))))
- (surrender-timeslice)
- (idle-loop))
-
- ; Running this procedure as a concurrent task improves interactive
- ; performance because
- ; (1) this procedure creates no garbage whatsoever (so running
- ; it as a task makes garbage collections occur less frequently);
- ; (2) all pending interrupts are accepted each time through the
- ; loop (because the time procedure enables interrupts).
-
- (define (relaxation-loop)
- (time)
- (relaxation-loop))
-
- ; The scheme-top-level procedure is called when MacScheme starts up.
-
- (define (scheme-top-level)
- ; exit if an error causes a reset
- (set! scheme-top-level exit)
- (main)
- (exit))
-
- (link-application)
-